package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/", login)
	http.ListenAndServe(":8080", nil)
}

func login(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Welcome to my test website!\n")
	// http://192.168.96.11:8080/?userName=admin&passWord=1234
	fmt.Println("request url path", r.URL.Path)
	fmt.Println("scheme", r.URL.Scheme)
	// 解析 url 传递的参数，对于 POST 则解析响应包的主体（request body)
	// 注意:如果没有调用 ParseForm 方法，下面无法获取表单的数据
	if r.ParseForm() != nil {
		fmt.Fprintf(w, "Parse http request Form Error!\n")
		return
	}
	fmt.Fprintf(w, "http.Request From is ", r.Form)
	fmt.Fprintf(w, "\n")
	fmt.Fprintf(w, "http.Request Header is ", r.Header)
	fmt.Fprintf(w, "\n")

	// 判断参数是否是Get请求，并且参数解析正常
	if r.Method == "GET" && r.ParseForm() == nil {
		// 接收参数
		userName := r.FormValue("userName")
		fmt.Printf("userName: %s \n", userName)
		passWord := r.FormValue("passWord")
		fmt.Printf("passWord: %s \n", passWord)
		if userName == "" || passWord == "" {
			w.Write([]byte("用户名或密码不能为空"))
		}
		if userName == "admin" && passWord == "1234" {
			w.Write([]byte("登录成功！"))
		} else {
			w.Write([]byte("用户名或密码错误！"))
		}
	}
}
